<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Advertise class
 *
 * Advertise setting information's CRUD.
 *
 */
class Advertise_lib
{
	/**
	 * Constructor
	 *
	 * Load all advertisements model.
	 */
	public function __construct()
	{
		$this->CI = &get_instance();
		
		$this->CI->load->model('pf/ad/ad_metaps_model');
	}
	
	/**
	 * Get advertisement setting regarding app_id and os
	 *
	 * This function could get advertisement setting regarding app_id and os.
	 *
	 * @param	integer	application id
	 * @param	integer os type
	 * @return	object
	 */
	public function get_ad_setting($app_id = 0, $chcode = 0)
	{
		$result = array();
		
		$metaps = $this->_get_metaps_setting($app_id, $chcode);
				
		if(!empty($metaps))
		{
			$result = array_merge($result, array('metaps' => $metaps));
		}
		
		// could append new advertise setting in the following
		
		return $result;
	}
	
	/**
	 * Get Metaps information regarding app_id and os
	 *
	 * This function could get Metaps information regarding app_id and os.
	 *
	 * @param	integer	application id
	 * @param	integer	os type
	 * @return	object
	 */
	private function _get_metaps_setting($app_id = 0, $chcode = 0)
	{
		$data = $this->CI->ad_metaps_model->get($app_id, $chcode);
		
		if(sizeof($data) > 0)
		{
			$now = time();
			
			if($data['start_t'] < $now && $data['end_t'] > $now)
			{
				$metaps = array(
							'appid' => $data['appid'],
							'appsecret' => $data['appsecret'],
							'useid' => $data['userid']
							);
		
				return $metaps;
			}
		}
		
		return null;
	}
}